//
//  handAlipay.m
//  beloved999
//
//  Created by Sam Feng on 15/9/19.
//  Copyright (c) 2015年 beloved999. All rights reserved.
//

#import "handAlipay.h"
#import "Order.h"
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>

@interface handAlipay()

@property (strong,nonatomic) NSDictionary * payDict;
@end


@implementation handAlipay
@synthesize payDict = _payDict;


/*=======================需要填写商户app申请的（必须填写）===================================*/
static NSString *partner = @"2088321022546780"; // @"2088311481441421";
static NSString *seller = @"apps@caranyi.com"; //@"1556480965@qq.com";
static NSString *privateKey = @"MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAM/PliSMnUKRQPAyvc+tGuo5ujvkKHkoz4KFvynlAV3WBMDtNbBPN7GTvO0PyzEqNl/36eF1Y0NKJtDRTf/wzPcVRqP3Ns221oruriHJpUHNPwX1oQk4InuHKXkFBsESbkL3hvnsDD81pqItYqFtpiRpaonSvFLccVGop6G4f3RRAgMBAAECgYAHjmXIOHCzbfKtiTrLpWC4KZcy/E3YcfpxpYlY7aHKFrEfjsCIA6ljxJp3CD78L6vAe5Q+hXUxii21JBcw+kGJnaeYJpw1hD7djt/fu/H/wxnbh7mU9uyxlY766VIoovM9Hpscmqz3o5MYDmzojGwUaL+4C10bu1tX9Lda0hOunQJBAPT0+t8zanV3tVUlOFmuGmSPfNAoz+D/eTWd0BoUtWjRwQpElom7FEKNjNxL81BBja54rLlscAW0noiBNs4WNf8CQQDZLeh0QUCdZRYVkTnQCM2yNQAdFAcChUArdJ6uBsids0ZSN2h0u7PBEL5aHcXtoKiwZzJjNLJWyGAvXxTtXXWvAkAPxPGditbQpxsyVwoQo+epD5+uWuI/DtNDw1wRQcIH9HDj81Rpm3SLcEdgKzeHtCCl2bjs2vE/PMhz6zWXANELAkBoftNC5IafdJzsoaSseR/np+K3GwTIbK7UeddDqpXqcvDZ7DkBObMiqNi96Wdrj4q63dx7qUZJxT2qbVhXYUmXAkEAkMsJNZ5F9Ou3WmeNjvLJfrJo6jWLnCnUQSmYrY6cmzP6Hvp2OAggyNT1mBgKvqgm5AJAD315+zUvpRF319D4WA==";
/*============================================================================*/


-(id)init:(NSDictionary *)dict{
    if (self = [super init])
    {
        _payDict = dict;
    }
    return self;
}


- (void)sendPay_demo
{
    if(_payDict == nil)
    {
        return;
    }
    
    Order *order = [[Order alloc] init];
//    order.partner =[_payDict objectForKey:@"alipay_partner"];//合作账号
//    order.sellerID = [_payDict objectForKey:@"alipay_account"];//商户号
//    order.outTradeNO = [_payDict objectForKey:@"pay_sn"]; //订单ID（由商家自行制定）
//    order.productName = @"HuiYun订单结算"; //商品标题
//    order.productDescription = @"商品描述"; //商品描述
//    order.amount = [_payDict objectForKey:@"order_amount"]; //商品价格
//    //    order.amount = @"0.01";
//    order.notifyURL =  [_payDict objectForKey:@"url"]; //回调URL
//    
//    order.service = @"mobile.securitypay.pay";
//    order.paymentType = @"1";
//    order.inputCharset = @"utf-8";
//    order.itBPay = @"30m";
//    order.showUrl = @"m.alipay.com";
    
    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types
    NSString *appScheme = @"huiyunB";
    
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    //NSLog(@"orderSpec = %@",orderSpec);
    
    //传入私钥并生成签名对象（外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode）
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    
    //使用签名方法对订单信息签名
    NSString *signedString = [signer signString:orderSpec];
    
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = nil;
    if (signedString != nil) {
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec,    //订单信息字符串
                       signedString, //签名后生成的字符串
                       @"RSA"];      //签名类型
        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic)
         {
             NSLog(@"reslut = %@",resultDic);
             NSNotification* notification = [NSNotification notificationWithName:@"payCallBack" object:resultDic userInfo:nil];
             [[NSNotificationCenter defaultCenter] postNotification:notification];
         }];
    }
}





@end
